package com.videoaggre.zcl.parse.impl

import com.videoaggre.zcl.bean.EnterdeskImage
import com.videoaggre.zcl.bean.MeizuoImage
import com.videoaggre.zcl.utils.Logger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup

object MeizuoParse {
    @JvmStatic
    suspend fun parseImages(html: String?) = withContext(Dispatchers.IO) {
        try {
            val enterdeskImages = mutableListOf<EnterdeskImage>()
            Jsoup.parse(html).select(".Left_bar li a")?.forEach {
                val href = it.attr("href")
                enterdeskImages.add(
                    EnterdeskImage(
                        it.selectFirst("img").attr("data-src"),
                        250,
                        350,
                        href.substring(href.lastIndexOf("_") + 1, href.lastIndexOf(".")),
                    )
                )
            }
            enterdeskImages
        } catch (e: Exception) {
            Logger.e(e)
            mutableListOf()
        }
    }

    fun parseDetailImage(html: String?): MeizuoImage =
        try {
            val root = Jsoup.parse(html)
            val ptitle = root.select(".ptitle").first().text()
            val pageParam = ptitle.substring(ptitle.lastIndexOf("（") + 1, ptitle.lastIndexOf("）")).split("/")
            MeizuoImage(root.select(".pic-meinv img").first().attr("src"), pageParam[0].toInt(), pageParam[1].toInt())
        } catch (e: Exception) {
            Logger.e(e)
            MeizuoImage()
        }
}